<template>
  <header>
    <div class="nav">
      <div class="logo">心理预约系统</div>
      <div class="nav-right">
        <!-- 只有来访者角色才显示查看历史问卷按钮 -->
        <button v-if="isLoggedIn && role === 'VISITOR'" class="nav-questionnaire" @click="viewHistoryQuestionnaire">查看历史问卷</button>
        <div v-if="isLoggedIn" class="dropdown">
          <button class="dropbtn">{{ userName }}</button>
          <div class="dropdown-content">
            <a href="/profile">个人资料</a>
            <a href="/appointments">我的预约</a>
            <a @click="logout">注销</a>
          </div>
        </div>
        <button v-if="!isLoggedIn" @click="goToLogin" class="nav-btn">登录</button>
        <button v-if="isLoggedIn" class="nav-btn" @click="goToMessages">
          消息 <span v-if="unreadCount > 0" class="unread-badge">{{ unreadCount }}</span>
        </button>
      </div>
    </div>
  </header>
</template>

<script>
export default {
    computed: {
    unreadCount() {
      return this.$store.state.messages.unreadTotal
    }
  },
  data() {
    return {
      isLoggedIn: localStorage.getItem("token") !== null,  // 判断是否登录
      role: localStorage.getItem("role") || "VISITOR",     // 获取用户角色，默认为来访者
      userName: localStorage.getItem("userName") || "我的", // 显示用户名
    };
  },
  methods: {
    // 查看历史问卷
    viewHistoryQuestionnaire() {
      this.$emit('view-history-questionnaire');
    },
    // 用户注销
    logout() {
      localStorage.removeItem("token");
      localStorage.removeItem("role");
      localStorage.removeItem("userName");
      this.$router.push("/login");  // 注销后跳转到登录页面
    },
    // 跳转到登录页面
    goToLogin() {
      this.$router.push("/login");
    },
    // 跳转到消息页面
    goToMessages() {
      this.$router.push("/messages"); // 跳转到消息页面
    },
  }
};
</script>

<style scoped>
/* 导航栏样式 */
header {
  margin-bottom: 30px;
}

.nav {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 15px;
  border-bottom: 2px solid #ddd; /* 细线分隔 */
}

.logo {
  font-size: 24px;
  font-weight: bold;
  color: #4D90FE; /* 明亮的蓝色 */
}

.nav-right {
  display: flex;
  align-items: center;
}

.nav-questionnaire {
  background-color: #4D90FE;
  color: white;
  padding: 9px 16px;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  font-size: 16px;
}

.nav-btn {
  background-color: #4D90FE;
  color: white;
  padding: 8px 16px;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  margin-left: 10px;
  transition: background-color 0.3s;
  font-size: 16px;
  position: relative;
}

.nav-btn:hover {
  background-color: #357AE8; /* 悬停时深蓝色 */
}

.dropdown {
  position: relative;
}

.dropbtn {
  background-color: #4D90FE;
  color: white;
  padding: 8px 16px;
  border: none;
  border-radius: 5px;
  cursor: pointer;
  font-size: 16px;
}

.dropbtn:hover {
  background-color: #357AE8;
}

.dropdown-content {
  display: none;
  position: absolute;
  top: 100%;
  left: 0;
  background-color: #fff;
  min-width: 160px;
  box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
  border-radius: 5px;
}

.dropdown:hover .dropdown-content {
  display: block;
}

.dropdown-content a {
  color: #333;
  padding: 12px 16px;
  text-decoration: none;
  display: block;
}

.dropdown-content a:hover {
  background-color: #f1f1f1;
}
.unread-badge {
  background: red;
  color: white;
  border-radius: 50%;
  padding: 2px 6px;
  font-size: 12px;
  margin-left: 5px;
}
</style>